package com.augurit.gzcgwsp.state.util;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
  
/**  
20. * 根据itext提供的java类库，构建word模板，并添加相应的内容，从而导出word报告；平台不相关  
21. * 需要引入iText-2.1.7.jar;iTextAsian.jar;iText-rtf-2.1.7.jar  
22. *   
23. * @author ryan  
24. */  
public class ItextWordUtil {     
 
	/**
	 * 创建文档
	 */
    public static Document ceateDocument(Rectangle rectangle){   
    		Document document = new Document(rectangle);   
           return document;
    }   
   /**  
     * @param filePath 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中  
     * @throws DocumentException  
     * @throws IOException  
     */  
   public  static void openDocument(Document document,String filePath) throws DocumentException,   
    IOException {   
       //建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中   
        RtfWriter2.getInstance(document, new FileOutputStream(filePath));   
       document.open();   
  
   }   
      
   /**  
     * @param titleStr 标题  
     * @param fontsize 字体大小  
     * @param fontStyle 字体样式  
     * @param elementAlign 对齐方式  
     * @throws DocumentException  
     */  
   public static void insertTitle(Document document,BaseFont bfChinese,String titleStr,int fontsize,int fontStyle,int elementAlign) throws DocumentException{   
       Font titleFont = new Font(bfChinese, fontsize, fontStyle);   
        Paragraph title = new Paragraph(titleStr);   
       // 设置标题格式对齐方式   
       title.setAlignment(elementAlign);   
       title.setFont(titleFont); 
       document.add(title);   
    }   
    /**  
     * @param contextStr 内容  
     * @param fontsize 字体大小  
     * @param fontStyle 字体样式  
     * @param elementAlign 对齐方式  
     * @throws DocumentException   
     */  
   public void insertContext(Document document,BaseFont bfChinese,String contextStr,int fontsize,int fontStyle,int elementAlign) throws DocumentException{   
       // 正文字体风格   
       Font contextFont = new Font(bfChinese, fontsize, fontStyle);   
       Paragraph context = new Paragraph(contextStr);   
      //设置行距   
        // 正文格式左对齐   
       context.setAlignment(elementAlign);   
       context.setFont(contextFont);   
      // 离上一段落（标题）空的行数   
       context.setSpacingBefore(5);   
      // 设置第一行空的列数   
      context.setFirstLineIndent(20);   
       document.add(context);   
    }   
   
   /**  
    * @param imgUrl 图片路径  
390.     * @param imageAlign 显示位置  
391.     * @param height 显示高度  
392.     * @param weight 显示宽度  
393.     * @param percent 显示比例  
394.     * @param heightPercent 显示高度比例  
395.     * @param weightPercent 显示宽度比例  
396.     * @param rotation 显示图片旋转角度  
397.     * @throws MalformedURLException  
398.     * @throws IOException  
399.     * @throws DocumentException  
400.     */  
    public static void insertImg(Document document,String imgUrl,int imageAlign,int height,int weight,int percent,int heightPercent,int weightPercent,int rotation) throws MalformedURLException, IOException, DocumentException{   
//       添加图片   
       Image img = Image.getInstance(imgUrl);   
        if(img==null)   
           return;   
        img.setAbsolutePosition(0, 0);   
        img.setAlignment(imageAlign);   
        img.scaleAbsolute(height, weight);   
        img.scalePercent(percent);   
        img.scalePercent(heightPercent, weightPercent);   
        img.setRotation(rotation);   
        document.add(img);   
   }   
    
    /**
     * 关闭文档
     * @param document
     * @throws DocumentException 
     * @date 2012-4-21
     */
   public static void closeDocument(Document document) throws DocumentException{   
        document.close();   
   }   
      
   /**
    * 创建table
    * @param row
    * @param col
    * @param width1
    * @param color
    * @return 
    * @date 2012-4-21
    */
   public static Table creatTeable(int row,int col,int[] width1,Color color){
	   Table table=null;
       try {
		   table = new Table(col,row);   
		   table.setWidths(width1);// 设置每列所占比例   
		   table.setWidth(100); // 占页面宽度 100% 
		   table.setAlignment(Element.ALIGN_CENTER);// 居中显示   
		   table.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示   
		   table.setAutoFillEmptyCells(true); // 自动填满   
		   table.setBorderWidth(0); // 边框宽度   
		   table.setBorderColor(color); // 边框颜色  
	} catch (Exception e) {
		e.printStackTrace();
	}
	return table;
   }
   /**
    * 表格插入图片
    * @param table
    * @param photo 
    * @date 2012-4-21
    */
	public static void createCellByImage(Table table, Image photo,Font fontChinese,int colspan,int rowspan,Color color) {
		  Cell cell3=null;
		try {
			cell3 = new Cell(photo);
		} catch (BadElementException e) {
			e.printStackTrace();
		}   
	       cell3.setVerticalAlignment(Element.ALIGN_CENTER);   
	       cell3.setHorizontalAlignment(Element.ALIGN_CENTER);   
	       cell3.setRowspan(rowspan); 
	       cell3.setColspan(colspan); 
	       cell3.setBorderColor(color);   
	       //cell3.setBackgroundColor(new Color(153, 204, 255));   
	       table.addCell(cell3);   
		
	}    
   /**
    * 插入表格
    * @param table
    * @param content
    * @param fontChinese
    * @param colspan
    * @param rowspan
    * @param color 
    * @date 2012-4-21
    */
   public static void createCell(Table table,String content,
		   Font fontChinese,int colspan,int rowspan,Color color,int vertical,int  horizontal){
	   try {
		  Cell cell = new Cell(new Phrase(content, fontChinese));   
		   cell.setVerticalAlignment(vertical);   
		   cell.setHorizontalAlignment(horizontal);   
		   cell.setColspan(colspan);   
		   cell.setRowspan(rowspan);  
		   cell.setBorderColor(color);   
		  //cell5.setBackgroundColor(new Color(204, 255, 255));   
		   table.addCell(cell);
	} catch (BadElementException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}    
   }

   
    public static void main(String[] args) throws DocumentException, IOException {   
    	Document document=ceateDocument(PageSize.A4);
    	openDocument(document,"E:\\dome1.doc");
    	BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",   
                "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 
    	//insertTitle(document,bfChinese,"收件通知书", 12, Font.BOLD, Element.ALIGN_CENTER); 
    	 int width[] = {20,20};   
    	Table table=creatTeable(3,2,width,new Color(0, 125, 255));
    	int width1[] = {10,20,10,10,10};
    	Table table1=creatTeable(11,5,width1,new Color(0, 125, 255));
    	Font fontChinese = new Font(bfChinese, 10); 
    	Font font = DocStyleUtils.setFontStyle("华文中宋", 14f, Font.BOLD);   
    	//createCell(table,"统计时段",fontChinese,7,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
    	//createCellByImage(table,photo,fontChinese,2,4,new Color(0, 0, 0));
    	StringBuffer contentSb = new StringBuffer();   
        contentSb.append("你单位向广州市城市管理委员会提出的关于的申请，提交材料明细如下：");   
        Paragraph blankRow = new Paragraph(18f, " ", fontChinese);   
        Paragraph bodyPar = DocStyleUtils.setParagraphStyle(contentSb.toString(), fontChinese, 32f, 18f);   
        StringBuffer contentSb1 = new StringBuffer();   
        contentSb1.append("依据《中华人民共和国行政许可法》及相关法律法规规定，本机关将在 5  个工作日内做出是否受理决定。");   
        Paragraph bodyPar1 = DocStyleUtils.setParagraphStyle(contentSb1.toString(), fontChinese, 32f, 18f);   
        StringBuffer contentSb2 = new StringBuffer();   
        StringBuffer contentSb3 = new StringBuffer(); 
        StringBuffer contentSb4 = new StringBuffer();  
        StringBuffer contentSb5 = new StringBuffer();  
        StringBuffer contentSb6 = new StringBuffer(); 
        StringBuffer contentSb7 = new StringBuffer(); 
        StringBuffer sqdwstr = new StringBuffer();
        StringBuffer title = new StringBuffer();   
        sqdwstr.append("广州测试公司："); 
        title.append("收件通知书");   
        Paragraph titlepar = DocStyleUtils.setParagraphStyle(title.toString(), font, 0f, 18f,Element.ALIGN_CENTER);   
        contentSb2.append("提示：1. 本通知书不作为是否受理行政许可申请的依据。");   
        Paragraph bodyPar2 = DocStyleUtils.setParagraphStyle(contentSb2.toString(), fontChinese, 0f, 18f);   
        contentSb3.append("2. 换领受理通知书时需出示本通知书和领取人身份证明。");   
        Paragraph bodyPar3= DocStyleUtils.setParagraphStyle(contentSb3.toString(), fontChinese, 34f, 18f);   
        contentSb4.append("3. 本窗口联系电话：020—");   
        Paragraph bodyPar4= DocStyleUtils.setParagraphStyle(contentSb4.toString(), fontChinese, 34f, 18f);   
        contentSb5.append("4. 网络查询请登陆http://www.gzcgw.gov.cn");   
        Paragraph bodyPar5= DocStyleUtils.setParagraphStyle(contentSb5.toString(), fontChinese, 34f, 18f);   
        contentSb6.append("5. 以上情况经确认属实。");   
        Paragraph bodyPar6= DocStyleUtils.setParagraphStyle(contentSb6.toString(), fontChinese, 34f, 18f);   
        contentSb7.append("申请经办人签名：               联系电话：                 签收日期：");   
        Paragraph bodyPar7= DocStyleUtils.setParagraphStyle(contentSb7.toString(), fontChinese, 34f, 18f);   
        Paragraph bodyParsqdw= DocStyleUtils.setParagraphStyle(sqdwstr.toString(), fontChinese, 0f, 18f);   
        
        Cell cell6 = new Cell(new Phrase("  完成时限", fontChinese));   
        cell6.setVerticalAlignment(Element.ALIGN_CENTER);   
        cell6.setHorizontalAlignment(Element.ALIGN_LEFT);   
        cell6.setBorderColor(new Color(0, 0, 0));   
        //cell6.setBackgroundColor(new Color(204, 255, 255));   
        table.addCell(cell6);  
        
        
        document.add(blankRow);  
        document.add(blankRow);
     	document.add(titlepar); 
        document.add(blankRow); 
        document.add(blankRow);  
    	document.add(table); 
    	document.add(bodyParsqdw);   
    	document.add(bodyPar);   
    	document.add(table1);   
    	document.add(bodyPar1); 
    	document.add(blankRow); 
    	document.add(blankRow);
    	document.add(blankRow);
    	document.add(bodyPar2);
    	document.add(bodyPar3);
    	document.add(bodyPar4);
    	document.add(bodyPar5);
    	document.add(bodyPar6);
    	document.add(blankRow);
    	document.add(blankRow);
    	document.add(blankRow);
    	document.add(bodyPar7);
        document.add(new Paragraph("\n")); 
    
    	closeDocument(document);
    	
    	
    	
    	
   }


    
   
    	
    
    
}  